<template>
	<view class="flex flex-column">
		<!-- 个人信息 -->
		<view @click="myEdit()" class="my-4 mx-3 flex align-center">
			<view class="flex flex-column align-center" style="position: relative">
				<view class="" v-if="Memberavatar != ''" style="position: absolute; bottom: -20rpx; right: 0rpx">
					<image v-if="Memberavatar" style="width: 40rpx" :src="Domain_IMG + Memberavatar" mode="widthFix">
					</image>
				</view>
				<image :src="userImage" class="rounded-circle flex-shrink" style="width: 120rpx; height: 120rpx"
					mode=""></image>
			</view>
			<view class="py-2 ml-2 flex align-center" style="height: 80rpx">
				<image :src="Domain_ICON + '/edit.png'" style="width: 35rpx; height: 35rpx" mode=""></image>
				<text class="font-md font-weight-bold text-ellipsis" style="width: 350rpx">{{ userName }}</text>
			</view>
		</view>
		<view @click="onbalance()" class="mx-3 my-3 p-3 bg-light rounded-lg"
			style="background-color: rgb(234, 224, 215)">
			<text class="font">我的卡包 >></text>
			<text style="position: absolute; right: 60rpx">&gt;</text>
		</view>
		<!-- 我的订单 -->
		<view class="mx-3 p-3 rounded-lg bg-white" style="position: relative">
			<text class="font-md font-weight-bold">我的订单</text>
			<text @click="onOrder(0)" class="font-sm" style="position: absolute; right: 30rpx; top: 30rpx">全部订单</text>
			<view class="pt-3 flex flex-wrap">
				<view v-for="(item, index) in Order" @click="onOrder(item.url)" :key="index"
					class="flex flex-column align-center justify-between" style="width: 25%; height: 100rpx">
					<view class="flex align-end" style="width: 50rpx; height: 50rpx">
						<image :src="Domain_ICON + item.icon" class="w-100" style="height: 50rpx;" mode="widthFix"></image>
					</view>
					<text class="font">{{ item.value }}</text>
				</view>
			</view>
		</view>
		<!-- 我的采购单 -->
		<view v-if="Dealer_Authority" class="mt-3 mx-3 p-3 bg-white rounded-lg" style="position: relative">
			<text class="font-md font-weight-bold">采购订单</text>
			<text @click="onProcure(0)" class="font-sm"
				style="position: absolute; right: 30rpx; top: 30rpx">全部采购单</text>
			<view class="pt-3 flex flex-wrap">
				<view v-for="(item, index) in Order" @click="onProcure(item.url)" :key="index"
					class="flex flex-column align-center justify-between" style="width: 25%; height: 100rpx">
					<view class="flex align-end" style="width: 50rpx; height: 50rpx">
						<image :src="Domain_ICON + item.icon" class="w-100" style="height: 50rpx;" mode="widthFix"></image>
					</view>
					<text class="font">{{ item.value }}</text>
				</view>
			</view>
		</view>
		<!-- 更多服务 -->
		<view class="mx-3 my-3 px-3 pt-3 bg-white rounded-lg" style="position: relative">
			<text class="font-md font-weight-bold">更多服务</text>
			<view class="pt-3 flex flex-wrap">
				<view v-for="(item, index) in MoreServices" :key="index" @click="onMoreServices(item)"
					class="mb-3 flex flex-column align-center justify-between" style="width: 25%; height: 100rpx">
					<view class="flex align-end" style="width: 50rpx; height: 50rpx">
						<image :src="Domain_ICON + item.icon" class="w-100" style="height: 50rpx;" mode="widthFix"></image>
					</view>
					<text class="font">{{ item.value }}</text>
				</view>
			</view>
		</view>
		<!-- 按需隐藏-开发人员专属 -->
		<view v-if="Employee_Authority == '1' ? true : false" class="mx-3 p-3 bg-white rounded-lg"
			style="position: relative">
			<view class="font-md font-weight-bold"> 员工消息订阅 </view>
			<view class="flex flex-wrap">
				<view v-for="(item, index) in developerData" @click="onMoreServices(item)" :key="index"
					class="mt-3 flex flex-column align-center justify-between" style="position: relative; width: 25%; height: 100rpx">
					<view class="flex align-end" style="width: 50rpx; height: 50rpx">
						<image :src="Domain_ICON + item.icon" class="w-100" style="height: 50rpx;" mode="widthFix"></image>
					</view>
					<text class="font">{{ item.value }}</text>
					<i v-if="item.type==0" class="bg-danger rounded-circle text-white px-1 font-sm"
						style="position: absolute; top: 0;right: 0;">{{item.total}}</i>
				</view>
			</view>
		</view>
		<!-- 底部切换 -->
		<tabsBottom :tabId="3" v-if="switch_index == 0"></tabsBottom>
		<tabsBottomMerchant :tabId="2" v-else></tabsBottomMerchant>
	</view>
</template>

<script>
	const app = getApp();
	import tabsBottom from "@/components/tabs/tabs-bottom.vue";
	import tabsBottomMerchant from "@/components/tabs/tabs-bottom-Merchant.vue";
	import {WeChatSeedMessage,getWeChatSeedMessageAll} from '@/api/WeChat.js';
	export default {
		components: {
			tabsBottom,
			tabsBottomMerchant
		},
		data() {
			return {
				//模拟数据头像
				Dealer_Authority: app.globalData.memberInfo.Dealer_Authority, //判断供应商身份
				portrait: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
				userName: app.globalData.memberInfo.Nick,
				userImage: app.globalData.memberInfo.Image,
				Memberavatar: app.globalData.memberInfo.Picture_URL,
				Domain_IMG: app.globalData.Domain_IMG,
				Domain_ICON: app.globalData.Domain_ICON, //全局变量域名
				Order: [{
						//我的订单栏目
						icon: "/daifukuan.png",
						value: "待付款",
						url: "1",
					},
					{
						icon: "/daifahuo.png",
						value: "待发货",
						url: "2",
					},
					{
						icon: "/daishouhuo.png",
						value: "待收货",
						url: "3",
					},
					{
						icon: "/shouhou.png",
						value: "售后",
						url: "4",
					},
				],
				MoreServices: [{
						type:1,
						icon: "/kefu2.png",
						value: "客服中心",
						url: "/pages/my/customerServiceCenter",
					},
					{
						type:1,
						icon: "/gerenxinxi2.png",
						value: "个人信息",
						url: "/pages/my/personal_information",
					},
					{
						type:1,
						icon: "/zhangdan2.png",
						value: "账单",
						url: "",
					},
					{
						type:1,
						icon: "/youhuiquan.png",
						value: "优惠券",
						// url:'/pages/ceshi/ceshi'
						url: "",
					},
					{
						type:1,
						icon: "/browse.png",
						value: "历史记录",
						url: "/pages/my/browse",
					},
					{
						type:1,
						icon: "/authen.png",
						value: "商户认证",
						url: "/pages/Merchant/DealerAuthen",
					},
					{
						type:2,
						icon: "/authen.png",
						value: "身份切换",
					}
				],
				developerData: [//type: 0订阅  1跳转 2切换
					// {
					// 	icon: "/shopaddorrdc.png",
					// 	value: "商品明细",
					// 	url: "/pages/developer/developer",
					// },
					// {
					// 	icon: "/shopaddorrdc.png",
					// 	value: "订单明细",
					// 	url: "/pages/developerOrder/developerOrder",
					// },
					{
						type:0,
						icon: "/shopaddorrdc.png",
						value: "销售日报",
						template_id:'FKjIPYOTUsWriDLjBdei5IVWzQjaEnO2GyzqGuQ_1fk',
						total:0,
						loading:false
					},
					{
						type:0,
						icon: "/shopaddorrdc.png",
						value: "订单提醒",
						template_id:'OElnMlOAGfyiwRPkWtK8uj1iXnCxgGlMmTxgU4-ZcoI',
						total:0,
						loading:false
					},{
						type:0,
						icon: "/shopaddorrdc.png",
						value: "发货通知",
						template_id:'C3Np3to2X2aAeQteUKaiFOeUUfLatq1RjK5-sG3hnwc',
						total:0,
						loading:false
					},{
						type:0,
						icon: "/shopaddorrdc.png",
						value: "代发货提醒",
						template_id:'_vyi0obPz2RtNSh-V_GyBeFlZ63Vv2WUKf5-rI3veWM',
						total:0,
						loading:false
					},{
						type:0,
						icon: "/shopaddorrdc.png",
						value: "发票提醒",
						template_id:'0J4yIIpumOu82gMjaPaXvsYK_gGv3Y1AKP4eeJdm5nY',
						total:0,
						loading:false
					},{
						type:0,
						icon: "/shopaddorrdc.png",
						value: "发票成功",
						template_id:'g7Od_iUiFFqMKa_XsD_1hFi4bJm2lVt0JqIP_VAaWW4',
						total:0,
						loading:false
					}
				],
				Employee_Authority: app.globalData.memberInfo.Employee_Authority,
				switch_index: app.globalData.switch_index,
				isjieliu:false,
				isjieliu2:false,
			};
		},
		onLoad() {
			this.userName = app.globalData.memberInfo.Nick;
			this.userImage = app.globalData.memberInfo.Image;
			this.getData();
		},
		methods: {
			async getData(){//查询订阅消息次数
				const res = await getWeChatSeedMessageAll({
					Open_ID:app.globalData.openid,
					App_ID:app.globalData.appid,
					Enterprise_Info_SerialNumber:app.globalData.enterprise
				})
				if(res.Status == 'success'){
					this.developerData.forEach(item=>{
						res.Data.forEach(ite=>{
							if(item.template_id == ite.template_id){
								item.total = ite.Quantity
							}
						})
					})
				}
			},
			onOrder(id) {//跳转到订单列
				uni.navigateTo({
					url: `/pages/my/order?id=${id}`,
				});
			},
			onProcure(id) {
				uni.navigateTo({
					url: `/pages/Merchant/procure_order?id=${id}`,
				});
			},
			onbalance() {
				//跳转到我的余额
				uni.navigateTo({
					url: "/pages/card/My_card_bag",
				});
				// uni.showToast({
				// 	title:'暂未开放',
				// 	icon:'none'
				// })
			},
			myEdit() {
				//跳转到个人信息
				uni.navigateTo({
					url: "/pages/my/personal_information",
				});
			},
			async setWeChatSeedMessage(template_id,callback){
				callback(await WeChatSeedMessage({
					Open_ID:app.globalData.openid,
					Data:JSON.stringify({
						template_id,
						Open_ID:app.globalData.openid,
						App_ID:app.globalData.appid,
						Quantity:1,
						Enterprise_Info_SerialNumber:app.globalData.enterprise,
					})
				}))
			},
			onMoreServices(item) {
				const that = this;
				if(item.type == 0){//订阅
					if(!item.loading){
						item.loading = true
						uni.requestSubscribeMessage({
							tmplIds: [item.template_id],
							success: (res) => {
								if(res[item.template_id]=='accept'){
									uni.showLoading({
										title: "加载中",
									});
									that.setWeChatSeedMessage(item.template_id,(res)=>{
										uni.hideLoading()
										uni.showToast({title: "订阅成功！",icon: "none",});
										that.getData()
									})
								}else if(res[item.template_id]=='reject'){
									uni.showToast({
										title: "已取消！",
										icon: "none",
									});
								} 
								item.loading = false
							},
							fail: (res) => { item.loading = false }
						});
					}
				}else if(item.type == 1){
					if (item.url == "") {uni.showToast({title: "暂未开放",icon: "none",}); return}
					uni.navigateTo({url:item.url});
				}else if(item.type == 2){//切换
					if (app.globalData.switch_index == 1) {
						uni.showModal({title: '提示',content: '是否切换到用户？',success: (res) => {
							if (res.confirm) {// 用户点击确定按钮
								app.globalData.switch_index = 0;
								uni.redirectTo({url: '/pages/index/index'});// 在这里执行确定按钮的逻辑
							} else if (res.cancel) {// 用户点击取消按钮
								console.log('用户点击取消');// 在这里执行取消按钮的逻辑
									
							}
						}})
					} else {
						uni.showModal({title: '提示',content: '是否切换到经销商？',success: (res) => {
							if (res.confirm) {// 用户点击确定按钮
								app.globalData.switch_index = 1;	// 在这里执行确定按钮的逻辑
								uni.redirectTo({url: '/pages/Merchant/index'});
							} else if (res.cancel) {// 用户点击取消按钮
								console.log('用户点击取消');// 在这里执行取消按钮的逻辑
							}
						}})
					}
				}
			},
		},
		
	};
</script>

<style>
	page {
		background-color: #f8f9fa;
	}
</style>